package com.gxuwz.medical.web.servlet;

import java.io.IOException;
import java.io.PrintWriter;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.ServletResponse;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.alibaba.fastjson.JSON;
import com.gxuwz.medical.dao.MemberDao;
import com.gxuwz.medical.domain.family.Family;
import com.gxuwz.medical.domain.house.House;
import com.gxuwz.medical.domain.member.Member;
import com.gxuwz.medical.domain.vo.AutocompleteModel;

public class MemberServlet extends BaseServlet {

	private static final long serialVersionUID = 1L;
	private MemberDao memberDao;
	@Override
	public void init() throws ServletException {
		memberDao=new MemberDao();
	}
	public void doGet(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		this.doPost(request, response);
	}

	public void doPost(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		String m = request.getParameter("m");
		if ("list".equals(m)) {
			process(request, response, "/page/member/member_open.jsp");
		}else if("open".equals(m)){
			process(request, response, "/page/member/member_list.jsp");
		}else if ("hzxx".equals(m)) {
			process(request, response, "/page/member/member_show.jsp");
		} else if ("input".equals(m)) {
			process(request, response, "/page/member/member_add.jsp");
		} else if ("add".equals(m)) {
			add(request, response);
		} else if ("get".equals(m)) {
			process(request, response, "/page/member/member_edit.jsp");
		} else if ("edit".equals(m)) {
			edit(request, response);
			process(request, response, "/page/member/member_list.jsp");
		} else if ("select".equals(m)) {
			select(request, response);
		} else if ("ckcy".equals(m)) {
			process(request, response, "/page/member/member_list.jsp");
		} else if ("delhz".equals(m)) {
			try {
				delhz(request, response);
				process(request, response, "/page/member/member_open.jsp");
			} catch (SQLException e) {
				e.printStackTrace();
			}
		} else if ("delcy".equals(m)) {
			try {
				delcy(request, response);
				process(request, response, "/page/member/member_open.jsp");
			} catch (SQLException e) {
				e.printStackTrace();
			} catch (Exception e) {
				e.printStackTrace();
			}
		}else if("find".equals(m)){
			try {
				find(request,response);
			} catch (Exception e) {
				e.printStackTrace();
			}
		}
	}

	protected void add(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		String name = request.getParameter("name");
		String familycode = request.getParameter("familycode");
		String idcard = request.getParameter("idcard");
		String sex = request.getParameter("sex");
		String birthday = request.getParameter("birthday");
		String nation = request.getParameter("nation");
		String health = request.getParameter("health");
		String culture = request.getParameter("culture");
		String relation = request.getParameter("relation");
		String farmer = request.getParameter("farmer");
		String work = request.getParameter("work");
		String phone = request.getParameter("phone");
		String ylzcode = request.getParameter("ylzcode");
//		String nhzcode = request.getParameter("nhzcode");
		String age = request.getParameter("age");
		String occupation = request.getParameter("occupation");
		String housecode = request.getParameter("housecode");
		

		Member member = new Member();

		try {
			member.addMember(ylzcode, familycode, housecode, idcard,
					name, sex, birthday, nation, health, culture, relation,
					farmer, work, phone, age, occupation);
			
			Family family = new Family();
			family.setFamilycode(familycode);
			family.countNumber(farmer);
			
			process(request, response, "/page/member/member_list.jsp");
			
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	protected void delhz(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException,
			SQLException {
		String familycode = request.getParameter("familycode");
		House h = new House();
		try {
			h.delhz(familycode);
		} catch (SQLException e) {
			e.printStackTrace();
		}
	}

	protected void delcy(HttpServletRequest request,
			HttpServletResponse response) throws Exception {
		String idcard = request.getParameter("id");
		Member m = new Member();
		try {
			m.delcy(idcard);
			Member member = new Member(idcard);
			String farmer = member.getFarmer();
			if("是".equals(farmer)){
				String farmer1 = "1";
				m.countNumber1(farmer1);
			}else if("否".equals(farmer)){
				String farmer1 = "0";
				m.countNumber1(farmer1);
			}
		} catch (SQLException e) {
			e.printStackTrace();
		}
	}

	protected void edit(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		String name = request.getParameter("name");
		String familycode = request.getParameter("familycode");
		String idcard = request.getParameter("idcard");
		String sex = request.getParameter("sex");
		String birthday = request.getParameter("birthday");
		String nation = request.getParameter("nation");
		String health = request.getParameter("health");
		String culture = request.getParameter("culture");
		String relation = request.getParameter("relation");
		String farmer = request.getParameter("farmer");
		String work = request.getParameter("work");
		String phone = request.getParameter("phone");
		String ylzcode = request.getParameter("ylzcode");
		String nhzcode = request.getParameter("nhzcode");
		String age = request.getParameter("age");
		String occupation = request.getParameter("occupation");
		String housecode = request.getParameter("housecode");
		Member member = new Member();

		try {
			member.editMember(nhzcode, ylzcode, familycode, housecode, idcard,
					name, sex, birthday, nation, health, culture, relation,
					farmer, work, phone, age, occupation);

		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	protected void select(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		try {
			// 定义一个sb来接收模糊查询的sql
			StringBuffer sb = new StringBuffer(1024);
			// form表单提交过来的用户输入的title模糊查询关键字
			String housemaster = request.getParameter("title");
			// System.out.println("housemaster:" + housemaster);
			
			// 如果housemaster有输入的时候，才拼接sql进行查询
			 if (housemaster != null && !"".equals(housemaster)) {
				sb.append(" housemaster like '%" + housemaster + "%'");
			}

			List<House> newsList = House.getNewsList(sb.toString());

			request.setAttribute("newsList", newsList);
			request.getRequestDispatcher("/page/member/member_open.jsp")
					.forward(request, response);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	
	/**
	 * 根据姓名查找个人信息列表
	 * @param req
	 * @param resp
	 * @throws Exception
	 */
	private void find(HttpServletRequest request, HttpServletResponse response)
			throws Exception{
		String town =request.getParameter("town");
//		System.out.println("town:"+town);
		String q =request.getParameter("q");
		//插件请求方式是GET，采用转码方法处理中文乱码
		String name =new String(q.getBytes("ISO8859-1"),"utf-8");
//		System.out.println("name:"+name);
	    List<Member> persons = memberDao.findByKeyWord(name, town);
	    //将查询结果列表转换为自动填充数据模型列表
	    List<AutocompleteModel> itemList=new ArrayList<AutocompleteModel>();
		for(Member m:persons){
			String itemId=m.getIdcard();
//			System.out.println("itemId:"+itemId);
			String itemName =m.getName();
//			String sex="1".equals(m.getSex())?"女":"男";
			String sex = m.getSex();
			String familycode = m.getFamilycode();
			Family family = new Family();
			family.load(familycode);
			String address = family.getAddress();
			String otherName = sex+"-"+address;
//			System.out.println("otherName:"+otherName);
			String itemNhzcode = m.getNhzcode();
			AutocompleteModel item=new AutocompleteModel(itemId, itemName,otherName);
			itemList.add(item);
		}
		//转换为JSON的格式
	    String respBody = JSON.toJSONString(itemList);
		response.setCharacterEncoding("utf-8");
		PrintWriter out = response.getWriter();
		out.print(respBody);
		out.flush();
		out.close();
	} 
	
}
